import httpRequest from '@/utils/http-request'
import { ElMessage } from 'element-plus'

/** 单个模板数据 */
export interface IUserRegisterParams {
  username: string
  password: string
}

/**
 * 注册用户
 */
export const userRegister = async (params: IUserRegisterParams) => {
  try {
    const { data } = await httpRequest.post(`/account/register`, params)
    return data
  } catch (err: any) {
    console.error('err: ', err)
    ElMessage.error(`注册失败: ${err?.message}`)
    return false
  }
}

/**
 * 登录
 */
export const userLogin = async (params: IUserRegisterParams) => {
  try {
    const { data } = await httpRequest.post(`/account/login`, params)
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('登录失败')
    return false
  }
}

/**
 * 获取用户信息
 */
export const getUserInfo = async () => {
  try {
    const { data } = await httpRequest.get(`/account/getUserInfo`)
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('获取用户信息失败')
    return {}
  }
}

/**
 * 根据分页获取任务列表
 */
export const getAITaskList = async ({ page, pageSize, empId }: any) => {
  try {
    const { data } = await httpRequest.get(`/tasks/page`, {
      params: {
        page,
        pageSize,
        empId,
      },
    })
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('获取图库列表失败')
    return {}
  }
}

/**
 * 获取当天还剩余生成的次数
 */
export const getRemainCount = async (params: { empId: number }) => {
  try {
    const { data } = await httpRequest.get(`/tasks/getCanUseCount`, {
      params: {
        empId: params.empId,
      },
    })
    return data
  } catch (err) {
    console.error('err: ', err)
    ElMessage.error('获取可用次数失败')
    return {}
  }
}
